package com.shanshi.activity;

import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.amap.api.location.AMapLocation;
import com.amap.api.location.AMapLocationClient;
import com.amap.api.location.AMapLocationClientOption;
import com.amap.api.location.AMapLocationClientOption.AMapLocationMode;
import com.amap.api.location.AMapLocationListener;
import com.bumptech.glide.Glide;
import com.google.gson.Gson;
import com.shanshi.activity.response.IndexInfo;
import com.shanshi.activity.tool.CustomDialog;


public class IndexActivity extends MainActivity {

	private TextView title2;
	private TextView changestore;
	private LocationManager locationManager;
	private String locationProvider;
	private RelativeLayout rl;
	private ImageView img;
	private ImageView list;
	
	//高德地图定位
	//声明AMapLocationClient类对象
	public AMapLocationClient mLocationClient = null;
	//声明定位回调监听器
	public AMapLocationListener mLocationListener = new AMapLocationListener() {
		@Override
		public void onLocationChanged(AMapLocation arg0) {
			// TODO Auto-generated method stub
			
		}
	};
	//声明AMapLocationClientOption对象
	public AMapLocationClientOption mLocationOption = null;
	private IndexInfo indexInfo;


	@Override
	protected void onCreate(Bundle savedInstanceState) {
		//*****************高德定位功能******////
		//初始化定位
		mLocationClient = new AMapLocationClient(getApplicationContext());
		
		//设置定位回调监听
		mLocationClient.setLocationListener(mLocationListener);
		
		//初始化AMapLocationClientOption对象
		mLocationOption = new AMapLocationClientOption();
		
		//设置定位模式为AMapLocationMode.Hight_Accuracy，高精度模式。
		mLocationOption.setLocationMode(AMapLocationMode.Hight_Accuracy);
		
		//设置定位模式为AMapLocationMode.Battery_Saving，低功耗模式。
		mLocationOption.setLocationMode(AMapLocationMode.Battery_Saving);
		
		//设置定位模式为AMapLocationMode.Device_Sensors，仅设备模式。不需要连接网络，只使用GPS进行定位，这种模式下不支持室内环境的定位，自 v2.9.0 版本支持返回地址描述信息。
		mLocationOption.setLocationMode(AMapLocationMode.Device_Sensors);
		
		//获取一次定位结果：该方法默认为false
		mLocationOption.setOnceLocation(true);

		//获取最近3s内精度最高的一次定位结果：设置setOnceLocationLatest(boolean b)接口为true，启动定位时SDK会返回最近3s内精度最高的一次定位结果。如果设置其为true，setOnceLocation(boolean b)接口也会被设置为true，反之不会，默认为false。
		mLocationOption.setOnceLocationLatest(true);
		
		//设置定位间隔,单位毫秒,默认为2000ms，最低1000ms。
		mLocationOption.setInterval(1000);
		
		//设置是否返回地址信息（默认返回地址信息）
		mLocationOption.setNeedAddress(true);
		
		//设置是否强制刷新WIFI，默认为true，强制刷新。mLocationOption.setWifiActiveScan(false);
		
		//设置是否允许模拟位置,默认为false，不允许模拟位置
		mLocationOption.setMockEnable(false);
		
		//单位是毫秒，默认30000毫秒，建议超时时间不要低于8000毫秒。
		mLocationOption.setHttpTimeOut(20000);
		
		//关闭缓存机制
		mLocationOption.setLocationCacheEnable(false);
		
		//给定位客户端对象设置定位参数
		mLocationClient.setLocationOption(mLocationOption);
		
		//启动定位
		mLocationClient.startLocation();
		
		//*********** ********************////
		
		
		  
		
		super.onCreate(savedInstanceState);
		setContentView(R.layout.index);
		

		title2 = (TextView) findViewById(R.id.title2);
		rl = (RelativeLayout) findViewById(R.id.index_title_layout);
		list = (ImageView) findViewById(R.id.list);
		rl.removeView(list);
		img = (ImageView) findViewById(R.id.index_img);
		changestore = (TextView) findViewById(R.id.changestore);
		rl.removeView(changestore);
		
		String result = getIntent().getStringExtra("result");
		if (!result.equals("error")) {
			indexInfo = new Gson().fromJson(result, IndexInfo.class);
			title2.setText(indexInfo.getShopName());
			Glide
			  .with(IndexActivity.this)
			  .load(indexInfo.getShopImg()).centerCrop()
			  .into(img);
		}
		if (indexInfo.getState()==3){
			showPhoneUsedDialog();
		}
	}
	
	
	public Location getLocation2(){
		//获取地理位置管理器  
        locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
        if(locationManager.isProviderEnabled(LocationManager.NETWORK_PROVIDER)){
        	locationProvider = LocationManager.NETWORK_PROVIDER;
        }else if(locationManager.isProviderEnabled(LocationManager.GPS_PROVIDER)){
        	locationProvider = LocationManager.GPS_PROVIDER;
        }else{
        	Toast.makeText(this, "没有可用的位置提供器", Toast.LENGTH_SHORT).show();
        }
        //获取Location  
        Location loc = locationManager.getLastKnownLocation(locationProvider);  
            //不为空,获取设备信息并发送请求  
        	return loc;
      }
	
	
	
	
	/***号码被新用户注册弹窗****/
	public void showPhoneUsedDialog() {
		CustomDialog.Builder builder = new CustomDialog.Builder(this);
        builder.setTitle("已退出登录");
        builder.setMessage1("原因是此号码已被新用户注册使用");
        builder.setMessage2("如果您之前账号存在重要信息，您可以用新号找回");
        builder.setPositiveButton("用新号找回", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {
				Intent intent=new Intent(IndexActivity.this,PhoneNumberCheckActivity.class);
				startActivity(intent);
            }  
        });  
  
        builder.setNegativeButton("不用了，谢谢",
                new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {  
                       System.exit(0);
                    }  
                });  
  
        CustomDialog customDialog = builder.create(R.layout.index_phone_used_popwin,
        		R.id.index_phoneUsedTitle,R.id.index_findByNewPhone,R.id.index_registNewUser,
        		R.id.messag1,R.id.message2);
        customDialog.show();
        
        //设置弹窗之外的部分不能点击
        customDialog.setCanceledOnTouchOutside(false);
	}
	
	
	/***用户更换设备登录弹窗****/
	public void showChangeEquipmentDialog() {
		CustomDialog.Builder builder = new CustomDialog.Builder(this);  
        builder.setTitle("已退出登录");
        builder.setMessage1("您的账号于14:00在XX设备登录");
        builder.setPositiveButton("确定", new DialogInterface.OnClickListener() {  
            public void onClick(DialogInterface dialog, int which) {  
                dialog.dismiss();  
//                //设置你的操作事项  
            }  
        });  
  
        CustomDialog customDialog = builder.create(R.layout.index_change_equipment,
        		R.id.index_changeequip_title,R.id.index_changeequip_sure,R.id.index_changeequip_cancel,
        		R.id.index_message1,0);
        customDialog.show();
        
        //设置弹窗之外的部分不能点击
        customDialog.setCanceledOnTouchOutside(false);
	}
	
	
	/***页面按钮跳转****/
	public void nextPage(View view) {
		Intent nextIntent = null;
		switch (view.getId()) {
		case R.id.changestore:
			nextIntent = new Intent(this,StoreInfo.class);
		break;
		case R.id.index_use:
				//nextIntent = new Intent(this, LoginActivity.class);
				nextIntent = new Intent(this, LoginActivity.class);
			break;
		case R.id.index_location:
			nextIntent = new Intent(this, LocationActivity.class);
		break;
		case R.id.index_suggest:
			nextIntent = new Intent(this,Suggest.class);
			nextIntent.putExtra("lastActivity",1);
		break;
		}
		startActivity(nextIntent);
	}
	


}
